package com.tem.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tem.product.entity.SysCategory;
import com.api.manage.pojo.product.SysCategoryBean;
import com.tem.product.mapper.SysCategoryMapper;
import com.tem.product.service.ISysCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tem.product.utils.LocalRedisUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统 服务实现类
 * </p>
 *
 * @author 陈辉
 * @since 2023-11-08
 */
@Service
public class SysCategoryServiceImpl extends ServiceImpl<SysCategoryMapper, SysCategory> implements ISysCategoryService {
    @Autowired
    private LocalRedisUtils localRedisUtils;
    public List<SysCategoryBean> treeBeans(SysCategoryBean request){
        QueryWrapper<SysCategory> queryWrapper = new QueryWrapper<>();
        boolean isSearch=false;
        if(request.getSystemLevelId()!=null){
            queryWrapper.lambda().eq(SysCategory::getSystemLevelId,request.getSystemLevelId());
        }
        if(request.getCategoryName()!=null&&request.getCategoryName().length()>0){
            queryWrapper.lambda().like(SysCategory::getCategoryName,request.getCategoryName());
            isSearch=true;
        }
        if(request.getCategoryCode()!=null&&request.getCategoryCode().length()>0){
            queryWrapper.lambda().eq(SysCategory::getCategoryCode,request.getCategoryCode());
            isSearch=true;
        }
        queryWrapper.orderByDesc("seq");
        List<SysCategory> list = this.list(queryWrapper);
        if(list==null||list.size()<1)return null;
        List<SysCategoryBean> beans = new ArrayList<>();
        SysCategoryBean ctb = null;
        for (SysCategory c:list){
            ctb = new SysCategoryBean();
            BeanUtils.copyProperties(c,ctb);
            ctb.setCategoryPathName(localRedisUtils.getPathName(c.getCategoryPath(),c.getSysCategoryId()));
            beans.add(ctb);
        }
        if(isSearch){
            return beans;
        }else{
            //return this.buildTree(beans,0L);
            return this.buildTree(beans,0L);
        }
    }
    /**
     * 新的递归方式
     * @param list
     * @param pid
     * @return
     */
    public List<SysCategoryBean> buildTree(List<SysCategoryBean> list, Long pid){
        List<SysCategoryBean> children = list.stream().filter(x -> x.getParentId().equals(pid)).collect(Collectors.toList());
        List<SysCategoryBean> subclass = list.stream().filter(x -> !x.getParentId().equals(pid)).collect(Collectors.toList());
        if(children.size() > 0){
            children.forEach(x -> {
                if(subclass.size() > 0){
                    buildTree(subclass,x.getSysCategoryId()).forEach(
                            y -> x.getChildren().add(y)
                    );
                }
            });
        }
        return children;
    }
    public String getPathName(String path,String myId){
        String[] ids = path.split("\\.");
        StringBuffer sbs = new StringBuffer();
        for(String s:ids){
            if(s.length()>0){
                if(s.equals(myId))continue;
                SysCategory sysCategory = this.getById(new Long(s));
                if(sysCategory!=null && sysCategory.getCategoryName()!=null && sysCategory.getCategoryName().length()>0) {
                    sbs.append(sysCategory.getCategoryName()).append("->");
                }
            }
        }
        if(sbs.length()>0) return sbs.toString();
        else return null;
    }
}
